#include "texture.h"

#include <iostream>

using namespace std;

Texture::Texture (GLint internalFormat, GLint format) {
    
    this->internalFormat = internalFormat;    
    this->format = format;
    
    this->generate();
    this->bind();
    this->setParameters();
    // default size 1x1, resize on window resize
    this->setSize(1,1);
}

Texture::~Texture () {}

GLuint Texture::getID () {
    return this->id;
}

void Texture::generate () {
    glGenTextures(1, &this->id);
}

void Texture::bind(GLenum texture) {
    glActiveTexture(texture);
    this->bind();
}

void Texture::bind () {
    glBindTexture(GL_TEXTURE_2D, this->id);
}

void Texture::setParameters () {
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);    
}

void Texture::setSize(unsigned int width, unsigned int height) {
    this->bind();
    
    glTexImage2D(
        GL_TEXTURE_2D, 
        0, 
        this->internalFormat, 
        width, 
        height, 
        0, 
        this->format, 
        GL_UNSIGNED_BYTE, 
        0
    );    
}

void Texture::setName (string name) {
    this->name = name;
}

string Texture::getName () {
    return this->name;
}
